/**
* Event services
*/
app.factory("taskServices", function($http, toolsServices) {
	console.info("start to register taskServices");
	var tasks = [];
	var personaltasks = [];
	return {
		add: function(callback, name, user) {
			console.log('taskServices - add a new task');
			$http.post(app.servicebase, { "service" : "addTask", "name" : name, "user" : user.Id }).
			success(function(data, status) 
			{
				console.info("addTask - call success"); 
				if(data.isFailed == true)
				{
					throw data.exception;
				}
				tasks.push(data.response);
				callback(tasks);
			})
			.
			error(function(data, status) 
			{
				console.error("addTask - call failed"); 	
				throw status + ' : ' + data;		
			});
		},
		update: function(callback, task) {
			console.log('taskServices - update task');
			task.State = (task.State == 0) ? 1 : 0;
			$http.post(app.servicebase, { "service" : "updateTask", "id" : task.Id, "status" : task.State }).
			success(function(data, status) 
			{
				console.info("updateTask - call success"); 
				if(data.isFailed == true)
				{
					throw data.exception;
				}
				for(var i = 0; i < tasks.length; i++) {
					var localtask = tasks[i];
					if(localtask.Id == task.Id) {
						tasks.splice(i, 1);
						break;
					}
				}
				tasks.push(task);
				callback(tasks);
			})
			.
			error(function(data, status) 
			{
				console.error("updateTask - call failed"); 	
				throw status + ' : ' + data;		
			});
		},
		deleteTask: function(callback, task) {
			console.log('taskServices - delete task');
			$http.post(app.servicebase, { "service" : "deleteTask", "id" : task.Id}).
			success(function(data, status) 
			{
				console.info("updateTask - call success"); 
				if(data.isFailed == true)
				{
					throw data.exception;
				}
				for(var i = 0; i < tasks.length; i++) {
					var localtask = tasks[i];
					if(localtask.Id == task.Id) {
						tasks.splice(i, 1);
						break;
					}
				}
				callback(tasks);
			})
			.
			error(function(data, status) 
			{
				console.error("updateTask - call failed"); 	
				throw status + ' : ' + data;		
			});
		},
		allTasks: function(callback) {
			console.log('taskServices - get all tasks');
			$http.post(app.servicebase, { "service" : "getAllTasks"}).
			success(function(data, status) 
			{
				console.info("getAllTasks - call success"); 
				if(data.isFailed == true)
				{
					throw data.exception;
				}
				tasks = data.response;
				callback(tasks);
			})
			.
			error(function(data, status) 
			{
				console.error("getAllTasks - call failed"); 	
				throw status + ' : ' + data;		
			});
		},
		personnalTasks: function(callback, userId) {
			$http.post(app.servicebase, { "service" : "getPersonalEvents", "userId" : userId}).
			success(function(data, status) 
			{
				console.info("getPersonalEvents - call success"); 
				if(data.isFailed == true)
				{
					throw data.exception;
				}
				personalevents = data.response;
				callback(personalevents);
			})
			.
			error(function(data, status) 
			{
				console.error("getPersonalEvents - call failed"); 	
				throw status + ' : ' + data;				
			});
		}
	};
});